package com.five.api.controller;

import com.five.controller.BaseController;
import com.five.api.service.CourseService;
import com.five.entity.Course;
import com.five.entity.User;
import com.five.vo.BaseResultVO;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: star
 * @Date: 2021-05-21 13:58
 */
@RestController
@CrossOrigin
@RequestMapping("course")
@Api(tags = "课程业务接口")
public class CourseController extends BaseController {
    @Autowired
    private CourseService courseService;

    private Logger logger = LoggerFactory.getLogger(CourseController.class);

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiOperation("根据需求查询课程接口")
    public BaseResultVO<List<Course>> query(@ApiParam("根据所需参数查询课程") Course course){

        List<Course> courses = courseService.findAll(course);

        return successResp(courses);
    }

    @RequestMapping(value = "queryByCharge",method = RequestMethod.POST)
    @ApiOperation("根据charge查询收费/免费的课程接口")
    public BaseResultVO<Course> queryByCharge(@ApiParam("1返回收费课程，0返回免费课程") @RequestBody Long charge){

        logger.debug("charge:{}",charge);
        List<Course> courses = courseService.queryByCharge(charge);

        return successResp(courses);
    }


    @RequestMapping(value = "queryByCourseClassify",method = RequestMethod.POST)
    @ApiOperation("根据课程类型id查询课程")
    public BaseResultVO<List<Course>> queryByCourseClassify(@ApiParam("课程类型id") @RequestBody Long CourseClassifyId){
        List<Course> courses = courseService.queryByCourseClassify(CourseClassifyId);

        return successResp(courses);
    }

    @RequestMapping(value = "queryByState",method = RequestMethod.POST)
    @ApiOperation("根据状态查询课程")
    public BaseResultVO<List<Course>> queryByState(@ApiParam("状态 0 上架、 1 下架、2 待上架") @RequestBody Long state){
        List<Course> courses = courseService.queryByState(state);

        return successResp(courses);
    }

    @RequestMapping(value = "queryByCourseId",method = RequestMethod.POST)
    @ApiOperation("根据课程id查询课程")
    public BaseResultVO<Course> queryByCourseId( @ApiParam("课程id") Long courseId, HttpServletRequest request){
        //todo 根据课程id获取课程
        System.out.println("controller id:{}"+courseId);
        Course course = courseService.queryById(request, courseId);
        return successResp(course);
    }
    @RequestMapping(value = "querByPopularCourses",method = RequestMethod.POST)
    @ApiOperation("查询热门视频业务")
    public BaseResultVO querByPopularCourses(){
        List<Course> courses = courseService.queryByViewCount();
        logger.info("courses:{}",courses);

        return successResp(courses);
    }

    @RequestMapping(value = "queryByFuzzy",method = RequestMethod.POST)
    @ApiOperation("模糊查询业务")
    public BaseResultVO queryByFuzzy(@ApiParam(value = "课程对象") Course course){
        List<Course> courses = courseService.queryByFuzzy(course);
        if (courses==null){
            return errorResp("查询的结果不存在");
        }
        return successResp(courses);

    }

}
